/*
 * Location.java
 *
 * Created on 24 de diciembre de 2005, 20:52
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package Sindarin.Common;
import java.awt.Color;
import java.util.StringTokenizer;


/**
 *
 * @author fran
 */
public class MyColor implements java.io.Serializable {
    public Color color;

    /** Creates a new instance of Location */
    public MyColor(String cad) {
        setColor(cad);
    }
    
    public MyColor(int r, int g, int b) {
        setColor(r,g,b);
    }
    
    public MyColor(Color color) {
        this.color = color;
    }
    
    public void setColor(String cad) {
        StringTokenizer str = new StringTokenizer(cad,"[ ,]\n\t\r");
	int r = (new Integer(str.nextToken()).intValue());
	int g = (new Integer(str.nextToken()).intValue());
	int b = (new Integer(str.nextToken()).intValue());
        color = new Color(r,g,b);
    }
    
    public void setColor(int r, int g, int b) {
        color = new Color(r,g,b);
    }
    
    public boolean equals(MyColor b) {
        return color.equals(b.color);
    }
    
    public MyColor add(MyColor op2) {
        Color bColor = op2.color;
        int r = color.getRed()+bColor.getRed();
        int g = color.getGreen()+bColor.getGreen();
        int b = color.getBlue()+bColor.getBlue();
        return new MyColor((r > 255 ) ? 255 : r, (g > 255 ) ? 255 : g, (b > 255 ) ? 255 : b);
    }
    
    public MyColor sub(MyColor op2) {
        Color bColor = op2.color;
        int r = color.getRed()-bColor.getRed();
        int g = color.getGreen()-bColor.getGreen();
        int b = color.getBlue()-bColor.getBlue();
        return new MyColor((r < 0 ) ? 0 : r, (g < 0 ) ? 0 : g, (b < 0 ) ? 0 : b);
    }
    
    public String toString() {
        return "[r, g, b] = [" + color.getRed() + ", " + color.getGreen() + ", " + color.getBlue() + "]";
    }
}
